package edu.bbu.license.client.android.gui.transfer.model;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.services.transfer.model.SimpleTransferObject;

public class FileTransferItem {

	private Handler mainHandler;
	private View fileTransferView;
	
	private ImageView statusIcon;
	private TextView nameTextView;
	private TextView sizeTextView;
	private ProgressBar progressBar;
	
	private boolean selected = false;
	
	private SimpleTransferObject transferObject;
	
	public FileTransferItem(Context context) {
		mainHandler = new Handler(context.getMainLooper());
		
		fileTransferView = LayoutInflater.from(context).inflate(R.layout.files_transfer_activity_item, null);
		statusIcon = (ImageView) fileTransferView.findViewById(R.id.files_transfer_activity_item_status);
		nameTextView = (TextView) fileTransferView.findViewById(R.id.files_transfer_activity_item_name);
		sizeTextView = (TextView) fileTransferView.findViewById(R.id.files_transfer_activity_item_size);
		progressBar = (ProgressBar) fileTransferView.findViewById(R.id.files_transfer_activity_item_progress);
	}
	
	public View getView() {
		return fileTransferView;
	}
	
	public void setTransferObject(SimpleTransferObject transferObject) {
		this.transferObject = transferObject;
	}
	
	public SimpleTransferObject getTransferObject() {
		return transferObject;
	}
	
	public void setFileName(final String fileName) {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				nameTextView.setText(fileName);	
			}
		});
	}
	
	public void setFileSize(final String fileSize) {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				sizeTextView.setText(fileSize);	
			}
		});
	}
	
	public void setProgressPercent(final int percent) {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				progressBar.setProgress(percent);
			}
		});
	}

	public void setSelected(boolean selected) {
		if(this.selected != selected) {
			this.selected = selected;
			
			if(selected) {
				fileTransferView.setBackgroundResource(R.drawable.files_transfer_item_selected_background);
			} else {
				fileTransferView.setBackgroundResource(R.drawable.files_transfer_item_background);
			}
		}
	}
	
	public boolean isSelected() {
		return selected;
	}
	
	public void setUploadStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_upload_icon);
			}
		});
	}
	
	public void setDownloadStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_download_icon);
			}
		});
	}
	
	public void setFinishedStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_finished_icon);
			}
		});
	}
	
	public void setPausedStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_paused_icon);
			}
		});
	}
	
	public void setStoppedStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_stopped_icon);
			}
		});
	}
	
	public void setCrashedStatus() {
		mainHandler.post(new Runnable() {
			
			@Override
			public void run() {
				statusIcon.setImageResource(R.drawable.files_transfer_item_status_crashed_icon);
			}
		});
	}
	
}
